import axios from "axios";
import {Base64} from "js-base64";
export default {
    async decode(searchStatus, condFirst, condList) {
        // throw new Error("API demo error")
        // eslint-disable-next-line no-unreachable

        const data = {
                "type": "authors",
                "must": [],
                "must_not": [],
                "should": [],
                "offset": searchStatus.offset,
                "size": 20,
                "condFirstRe" : condFirst,
                "condListRe" : condList,
                "searchStatusRe": searchStatus,
                "tab" : 1,
        };
        let type = searchStatus.searchType;
        if(type === 1)  {
            data.sort = {
                "h_index": "desc",
            }
        }
        else if(type === 2) {
            data.sort = {
                "n_citation": "desc",
            }
        }
        else if(type === 3) {
            data.sort = {
                "n_pubs": "desc",
            }
        }

        if(condFirst.content) {
            if(condFirst.key === "关键词") {
                data.must.push({
                    "all": condFirst.content,
                })
            }
            else if(condFirst.key === "姓名") {
                data.must.push({
                    "name": condFirst.content,
                })
            }
            else {
                data.must.push({
                    "orgs": condFirst.content,
                })
            }
        }
        for (let i = 0; i < condList.list.length; i ++) {
            let tmp = condList.list[i];
            if(!tmp[2]) continue;
            var q;
            if(tmp[1] === "关键词") {
                q = {
                    "all": tmp[2],
                }
            }
            else if(tmp[1] === "姓名") {
                q = {
                    "name": tmp[2],
                }
            }
            else {
                q = {
                    "orgs": tmp[2],
                }
            }
            if(tmp[0] === "AND") {
                data.must.push(q);
            }
            else if(tmp[0] === "OR") {
                data.should.push(q);
            }
            else {
                data.must_not.push(q);
            }
        }

        // console.log(data)
        var str = JSON.stringify(data)
        // console.log(str)
        str = Base64.encode(str);
        // console.log(str)

        // str = Base64.decode(str)
        // console.log(str)

        return {
            str: str,
        }
    },

    async get(str) {
        // console.log(data)
        var res;
        var data = JSON.parse(Base64.decode(str))

        var query = {

        };
        query.must = data.must
        query.must_not = data.must_not
        query.should = data.should
        query.offset = data.offset
        query.size = data.size
        query.type = data.type
        if(data.sort) query.sort = data.sort
        if(data.range) query.range = data.range

        // console.log(data)


        await axios.post('/api/scholar/authors/pseudo_search/', query, {headers: {'Content-Type': 'application/json; charset=utf-8'}}
        ).then(function (response) {
            res = response
            // console.log(response);
        })
        // console.log(res)

        return {
            data: res.data.data,
            userList: res.data.data.data.map(x=> {
                return {
                    id: x.id?x.id:null,
                    name: x.name?x.name:'',
                    n_pubs:x.n_pubs?x.n_pubs:0,
                    avatar:x.avatar?x.avatar:null,
                    h_index:x.h_index?x.h_index:null,
                    position:x.position?x.position:null,
                    tags:x.tags?x.tags:null,
                    pubs:x.pubs?x.pubs:null,
                    org:x.org?x.org : null,
                    orgs:x.orgs?x.orgs:null,
                    work:x.work?x.work:null,
                    edu:x.edu?x.edu:null,
                    n_citation:x.n_citation?x.n_citation:null,
                    bio:x.bio?x.bio:null,
                }
            }),
        }
    }
}